import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getListSLA, deleteSLA, disableSLA } from '../../../api/sla'
export default function () {
  const loading = ref(false)
  const name = ref('')
  const total = ref(0)
  const tableDataList = ref(null)
  const remindModeList = ref([
    { name: '系统消息', val: '1' },
    { name: '微信', val: '2' },
    { name: '短信', val: '3' },
  ])
  const pagination = ref()

  // 获取SLA列表
  const getnetworkListRows = (val=true) => {
    loading.value = true
    if(val)pagination.value.setPage()
    const data = {
      name: name.value, //SLA名称
      page: pagination.value.current, //当面页码
      limit: pagination.value.size, //每页显示数量
    }
    getListSLA(data).then((res) => {
      if (res.data.code == 20000) {
        tableDataList.value = res.data.data.rows
        total.value = Number(res.data.data.total)
      } else {
        ElMessage({
          showClose: true,
          message: res.data.message,
          type: 'error',
          duration: 5000,
          plain: true,
        })
      }
      loading.value = false
    })
  }
  const ids = ref([])
  async function changeAll(n: number, str: string) {
    if (ids.value.length == 0) {
      ElMessage({
        message: `请选择要${str}的SLA`,
        type: 'warning',
        duration: 5000,
        plain: true,
        showClose: true,
      })
      return
    }
    ElMessageBox({
      //
      title: '提示',
      message: `确认${str}的该SLA?`,
      showCancelButton: true,
      closeOnClickModal: false,
      type: n === 3 ? 'error' : 'warning',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      autofocus: false,
    })
      .then(async () => {
        loading.value = true
        switch (n) {
          case 1:
          case 2:
            await disableSLA(ids.value, n == 1 ? false : true).then((res) => {
              ElMessage({
                showClose: true,
                message: res.data.message,
                type: res.data.success ? 'success' : 'error',
                duration: 5000,
                plain: true,
              })
              if (res.data.success) {
                ids.value = []
                getnetworkListRows()
              }
              loading.value = false
            })
            break
          case 3:
            await deleteSLA(ids.value).then((res) => {
              ElMessage({
                showClose: true,
                message: res.data.message,
                type: res.data.success ? 'success' : 'error',
                duration: 5000,
                plain: true,
              })
              if (res.data.success) {
                ids.value = []
                getnetworkListRows()
              }
              loading.value = false
            })
            break
        }
      })
      .catch()
  }
  function handleChange(val) {
    ids.value = []
    val.filter((o) => {
      ids.value.push(o.id)
    })
  }

  function valueChange() {
    getnetworkListRows()
  }
  return {
    handleChange,
    valueChange,
    changeAll,
    ids,
    remindModeList,
    loading,
    getnetworkListRows,
    total,
    pagination,
    tableDataList,
    name,
  }
}
